package com.api.db;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Hashtable;

public class DBUtils {
	static DBConnection dbConnection = new DBConnection();

	/**
	 * 获取表所有记录
	 * 
	 * @return
	 */
	public static Hashtable<Integer, HashMap<String, String>> selectSQL(String sql) {
		ResultSet resultSet = null;
		ResultSetMetaData colume = null;
		Hashtable<Integer, HashMap<String, String>> hashmap = new Hashtable<Integer, HashMap<String, String>>();
		Connection conn = null;

		try {
			conn = dbConnection.getConnection();
			Statement statement = conn.createStatement();
			resultSet = statement.executeQuery(sql);
			colume = resultSet.getMetaData();

			int key = 0;
			while (resultSet.next()) {
				HashMap<String, String> list = new HashMap<String, String>();
				for (int i = 1; i <= colume.getColumnCount(); i++) {
					list.put(colume.getColumnName(i), resultSet.getString(i));
				}
				hashmap.put(key, list);
				key++;
			}
			resultSet.close();
			statement.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return hashmap;
	}

	/**
	 * 更新
	 * 
	 * @param sql
	 */
	public static void updateSQL(String sql) {
		Connection connection;
		try {
			connection = dbConnection.getConnection();

			Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
					ResultSet.CONCUR_UPDATABLE);
			statement.executeUpdate(sql);
			statement.close();
			connection.close();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

}
